package com.stone.business.cllyjl.action;
/**
 * ----材料领用  ACTION---
 */
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.cllyjl.pojo.TClkcmx;
import com.stone.business.cllyjl.pojo.TCllyjl;
import com.stone.business.cllyjl.service.CllyjlService;
import com.stone.business.gzjl.pojo.TabGzjl;
import com.stone.business.gzrwd.pojo.TGzrwd;
import com.stone.business.gzrwd.pojo.TGzrwdRw;
import com.stone.business.workcard.pojo.MainWorkCardOne;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.pojo.TUser;
import com.stone.sys.service.DictionaryService;
import com.stone.utils.DateFormatTool;
import com.stone.utils.JsonUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.SpringUtil;

public class CllyjlAction extends BaseAction{
	private CllyjlService cllyjlService;
	private TCllyjl entity;
	private String gzrwdid;//工作任务单id
	private String id;
	private String status;
	private List<TClkcmx> clkcmxList;
	// 任务列表
	private List<TGzrwdRw> listRwd = null;
	private String hiddenids;
	private List list;
	private String datafrom;
	private String showtype;
	private String directFlag;
	private String cllb;
	private String clmcPram;
	//材料类别
	private List<TDictionary> cllbList;
	//材料名称
	private List<TDictionary> clmcList;
	//材料单位
	private List<TDictionary> cldwList;
	//材料型号
	private List<TDictionary> clxhList;
	//仓库名称
	private List<TDictionary> ckmcList;
	/**
	 * 进入材料领用记录详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initCllyjlDetail() throws AppException {
		if (!StringUtils.isNullOrBlankFull(gzrwdid)) {//来自工单
			if (!StringUtils.isNullOrBlankFull(gzrwdid)&&!"0".equals(gzrwdid.toString())) {//来自工单
		// 工作任务单主表
		TGzrwd gzrwd = (TGzrwd) cllyjlService.findById(Long.parseLong(gzrwdid), TGzrwd.class
				.getName());
		this.entity = (TCllyjl)cllyjlService.findFirst(" from TCllyjl where gzrwdid = '"
				+ gzrwd.getId() + "'");
		//by wangtao update20120523新建默认为系统当前用户日期
		if(entity==null){
			this.entity = new TCllyjl();
			clkcmxList=new ArrayList<TClkcmx>();
			 String sysdate = cllyjlService.getSysdate("yyyy-MM-dd");
			 this.entity.setStartdate(gzrwd.getCzkssj());
			 this.entity.setEnddate(gzrwd.getCzjssj());
		    entity.setLyrname(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			entity.setLyr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId()+"");
			//entity.setClyname(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			//entity.setCly(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId()+"");
			this.entity.setLydate(DateFormatTool.dateFormatDate(gzrwd.getCzkssj(), false));
			this.entity.setStationid(gzrwd.getBdz());
			this.entity.setStationname(gzrwd.getBdzname());
			this.entity.setFzr(gzrwd.getZbz());
			this.entity.setFzrname(gzrwd.getZbzname());
			this.entity.setCy(gzrwd.getZby());
			this.entity.setCyname(gzrwd.getZbyname());
			// 任务
			 listRwd = cllyjlService.findListByHql("from TGzrwdRw where gzrwdid = '" + gzrwdid + "'");
			// 总任务
			String rw = "";
			for (int index=0; index<listRwd.size();index++) {
				rw += listRwd.get(index).getSbname()+listRwd.get(index).getGznr() + "\n";
			}
			if (rw != "") {
				rw.substring(0,rw.length()-2);
			}
			this.entity.setWorkcontent(rw);
			this.entity.setStatus("0");
			this.entity.setDantype("1");
			this.entity.setGzrwdid(Long.parseLong(gzrwdid));
			this.entity.setOuid(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE).toString());
			// by wangtao update20120523
			String sysdateymd = cllyjlService.getSysdate("yyyyMMdd");
			List<HashMap<String, Object>> list = cllyjlService.findListBySql("select MAX(bh) as bh from TAB_CLLYJL where dantype='1' and bh like '" + sysdateymd + "%'");
			
			// 票号取得
			if (list.get(0).get("bh") == null || "".equals(list.get(0).get("bh"))) {
				this.entity.setBh(sysdateymd + "0001");
				
			} else {
				if(this.entity.getBh()==null||this.entity.getBh().equals("")){
				this.entity.setBh(String.valueOf(Long.parseLong((String)list.get(0).get("bh")) + 1 ));
			
				}
			
			}	
		}else{
		//this.entity = (TCllyjl) cllyjlService.findById(Long.parseLong(id),TCllyjl.class.getName());
		clkcmxList=cllyjlService.findListByHql("from TClkcmx where parentid='"+this.entity.getId()+"'");
		}
			}
		}
		// 修改进入
		else if (!"0".equals(id.toString())) {
			
			this.entity = (TCllyjl) cllyjlService.findById(Long.parseLong(id),TCllyjl.class.getName());
			clkcmxList=cllyjlService.findListByHql("from TClkcmx where parentid='"+this.entity.getId()+"'");

		// 新建进入
		} else {
			
			this.entity = new TCllyjl();
			clkcmxList=new ArrayList<TClkcmx>();
			 String sysdate = cllyjlService.getSysdate("yyyy-MM-dd");
			 this.entity.setStartdate(DateFormatTool.formatToDate(sysdate+" 09:00",true));
			 this.entity.setEnddate(DateFormatTool.formatToDate(sysdate+" 18:00",true));
			this.entity.setStatus("0");
			this.entity.setDantype(super.getSearchBean().getCon6());
			this.entity.setOuid(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE).toString());
			// by wangtao update201201430
			String sysdateymd = cllyjlService.getSysdate("yyyyMMdd");
			List<HashMap<String, Object>> list = cllyjlService.findListBySql("select MAX(bh) as bh from TAB_CLLYJL where dantype="+super.getSearchBean().getCon6()+" and bh like '" + sysdateymd + "%'");
			
			// 票号取得
			if (list.get(0).get("bh") == null || "".equals(list.get(0).get("bh"))) {
				this.entity.setBh(sysdateymd + "0001");
				
			} else {
				if(this.entity.getBh()==null||this.entity.getBh().equals("")){
				this.entity.setBh(String.valueOf(Long.parseLong((String)list.get(0).get("bh")) + 1 ));
			
				}
			
			}
		}
		
		//初始化材料相关列表 added by cheng 2012-5-21
		initClList();
		
		if(directFlag==null||directFlag.equals("")){
			return "CKD_SUCCESS";
		}else{
			return directFlag;
		}
		
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws AppException
	 */
	public String saveCllyjl() throws AppException {
       String sysdate = cllyjlService.getSysdate("YYYY-MM-DD");
		Date confDate = DateFormatTool.formatToDate(cllyjlService.getSysdate("yyyy-MM-dd HH24:mi"), true);
		if (this.entity.getId() == 0) {
			this.entity.setStatus("2");//待审核
			this.entity.setOuid(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE).toString());
		
		}
		// by wangtao update201201430
		String sysdateymd = cllyjlService.getSysdate("yyyyMMdd");
		List<HashMap<String, Object>> list = cllyjlService.findListBySql("select MAX(bh) as bh from TAB_CLLYJL where dantype='"+this.entity.getDantype()+"' and bh like '" + sysdateymd + "%'");
		
		// 票号取得
		if (list.get(0).get("bh") == null || "".equals(list.get(0).get("bh"))) {
			this.entity.setBh(sysdateymd + "0001");
			
		} else {
			if(this.entity.getBh()==null||this.entity.getBh().equals("")){
			this.entity.setBh(String.valueOf(Long.parseLong((String)list.get(0).get("bh")) + 1 ));
		
			}
		
		}
		String forword="";
		if(super.getSearchBean().getCon6().equals("2")||"2".equals(this.entity.getDantype())){
			forword="RKD_SUCCESS";
			}else{
				forword="CKD_SUCCESS";
			}
		String rtn = cllyjlService.doSave(this.entity, this.clkcmxList);
		
		super.setInfo(rtn);
		//初始化材料相关列表 added by cheng 2012-5-21
		initClList();
		return forword;

	}
	
	/**
	 * 删除
	 * 
	 * @return
	 * @throws AppException
	 */
	public String deleteCllyjl() throws AppException {
		
		cllyjlService.doDelAll(id, TCllyjl.class.getName());
		cllyjlService.deleteByDHql("parentid in (" + id + ")", null, TClkcmx.class.getName());
		
		return "DELETE_SUCCESS";
	}
	/**
	 * 删除材料明细
	 * 
	 * @return
	 * @throws AppException
	 */
	public void deleteDetail() throws Exception {
		cllyjlService.doDelAll(id, TClkcmx.class.getName());
	    super.getResponse().getWriter().print("{msg:'删除成功'}");
		}
	/**
	 * 提交 直接审核
	 * 
	 * @return
	 * @throws AppException
	 */
	public String confirmCllyjl() throws AppException {
		cllyjlService.updateByDHql("status = '" + super.getRequest().getParameter("flag") + "'", "id in (" + this.id + ")", null, null, TCllyjl.class.getName());
		return "DELETE_SUCCESS";	
	}
	/**
	 * 审核通过
	 * @return
	 */
	public String verify(){
		if(!StringUtils.isNullOrBlankFull(id)) {
			TUser tUser=(TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);
			cllyjlService.update(" status='1',shrid='"+tUser.getId()+"',shrname='"+tUser.getName()+"'"," id='"+id+"'",TCllyjl.class.getName());		
		}else{//来自工单审核
			TUser tUser=(TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);
			cllyjlService.update(" status='1',shrid='"+tUser.getId()+"',shrname='"+tUser.getName()+"'"," gzrwdid='"+gzrwdid+"'",TCllyjl.class.getName());		
		
		}
		//初始化材料相关列表 added by wangtao 2012-5-25
		initClList();
		String info="审核成功!";
		super.setInfo(info);
		return "CKD_SUCCESS";
	}	
	/**
	 * 材料领用
	 * @return
	 */
	public String clly(){
		if(!StringUtils.isNullOrBlankFull(id)) {
			TUser tUser=(TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);
			cllyjlService.update(" cly='"+tUser.getId()+"',clyname='"+tUser.getName()+"'"," id='"+id+"'",TCllyjl.class.getName());		
		}else{//来自工单材料领用
			TUser tUser=(TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);
			cllyjlService.update(" cly='"+tUser.getId()+"',clyname='"+tUser.getName()+"'"," gzrwdid='"+gzrwdid+"'",TCllyjl.class.getName());		
		
		}
		//初始化材料相关列表 added by wangtao 2012-5-25
		initClList();
		String info="材料领用成功!";
		super.setInfo(info);
		return "CKD_SUCCESS";
	}	
	/**
	 * 进入详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initRkdDetail() throws AppException {
	       
		// 修改进入
		if (!"0".equals(id.toString())) {

			this.entity = (TCllyjl) cllyjlService.findById(Long.parseLong(id),TCllyjl.class.getName());
			clkcmxList=cllyjlService.findListByHql("from TClkcmx where parentid='"+this.entity.getId()+"'");

		// 新建进入
		} else {
			
			this.entity = new TCllyjl();
			clkcmxList=new ArrayList<TClkcmx>();
			 String sysdate = cllyjlService.getSysdate("yyyy-MM-dd");
			 this.entity.setRkdate(DateFormatTool.formatToDate(sysdate+" 09:00",true));
			this.entity.setStatus("0");
			this.entity.setDantype(super.getSearchBean().getCon6());
			this.entity.setOuid(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE).toString());
			// by wangtao update201201430
			String sysdateymd = cllyjlService.getSysdate("yyyyMMdd");
			List<HashMap<String, Object>> list = cllyjlService.findListBySql("select MAX(bh) as bh from TAB_CLLYJL where bh like '" + sysdateymd + "%'");
			
			// 票号取得
			if (list.get(0).get("bh") == null || "".equals(list.get(0).get("bh"))) {
				this.entity.setBh(sysdateymd + "0001");
				
			} else {
				if(this.entity.getBh()==null||this.entity.getBh().equals("")){
				this.entity.setBh(String.valueOf(Long.parseLong((String)list.get(0).get("bh")) + 1 ));
			
				}
			
			}
		}
		
		//初始化材料相关列表 added by cheng 2012-5-21
		initClList();
		return "RKD_SUCCESS";
		
	}

	/**
	 * 保存、审核
	 * 
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {
		
		String   selectpage=super.getSearchBean().getCon1();
		
		Date confDate = DateFormatTool.formatToDate(cllyjlService.getSysdate("yyyy-MM-dd HH24:mi"), true);
		if (this.entity.getId() == 0) {
			
			this.entity.setOuid(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE).toString());
		}
		String rtn = cllyjlService.doSave(this.entity, this.clkcmxList);
		//初始化材料相关列表 added by wangtao 2012-5-22
		initClList();
		super.setInfo(rtn);

		return SUCCESS;

	}
	public void getClkcmx() throws IOException, AppException {

		List<Object> valsList = new ArrayList<Object>();
		HashMap<String, Object> m = new HashMap<String, Object>();
		//by wangtao update20120428
		String	sql  = 	"select (select t.ITEM_NAME from SYS_DICTIONARY t where t.item_type='CLLB' and t.ITEM_ID=mx.cllb) as cllb,(select t.ITEM_NAME from SYS_DICTIONARY t where t.item_type='CLMC' and t.ITEM_ID=mx.clmc) as clmc,lysl as lysl,(select t.ITEM_NAME from SYS_DICTIONARY t where t.item_type='CLDW' and t.ITEM_ID=mx.cldw) as cldw, (select t.ITEM_NAME from SYS_DICTIONARY t where t.item_type='CLXH' and t.ITEM_ID=mx.clxh) as clxh, (select t.ITEM_NAME from SYS_DICTIONARY t where t.item_type='KFMC' and t.ITEM_ID=mx.ckmc) as ckmc,TO_CHAR(ccdate,'YYYY-MM-DD') as ccdate,sccj as sccj" +
		" from tab_clkc_mx mx" +
		" where parentid = " + id;
	list = cllyjlService.findListBySql(sql);
	JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
      JSON json = JSONSerializer.toJSON(list, jsonConfig);
     super.getResponse().getWriter().print(json.toString());
		
	}
	public void getClmc() throws IOException, AppException {
		DictionaryService dicService = (DictionaryService) SpringUtil.getInstance().getBean("dicService");
		clmcList = dicService.findClmcList(cllb);
	JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
      JSON json = JSONSerializer.toJSON(clmcList, jsonConfig);
     super.getResponse().getWriter().print(json.toString());
		
	}
	////////////////////by wangtao add20130123根据材料名称级联材料单位
	public void getCldw() throws IOException, AppException {
		DictionaryService dicService = (DictionaryService) SpringUtil.getInstance().getBean("dicService");
		cldwList = dicService.findCldwList(clmcPram);
	    JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
        JSON json = JSONSerializer.toJSON(cldwList, jsonConfig);
        super.getResponse().getWriter().print(json.toString());
		
	}
	///////////////////////////
	public void setCllyjlService(CllyjlService cllyjlService) {
		this.cllyjlService = cllyjlService;
	}

	public TCllyjl getEntity() {
		return entity;
	}

	public void setEntity(TCllyjl entity) {
		this.entity = entity;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}


	public String getHiddenids() {
		return hiddenids;
	}

	public void setHiddenids(String hiddenids) {
		this.hiddenids = hiddenids;
	}
	
	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public String getDatafrom() {
		return datafrom;
	}

	public void setDatafrom(String datafrom) {
		this.datafrom = datafrom;
	}

	public String getShowtype() {
		return showtype;
	}

	public void setShowtype(String showtype) {
		this.showtype = showtype;
	}

	public CllyjlService getCllyjlService() {
		return cllyjlService;
	}

	public List<TClkcmx> getClkcmxList() {
		return clkcmxList;
	}

	public void setClkcmxList(List<TClkcmx> clkcmxList) {
		this.clkcmxList = clkcmxList;
	}

	public String getDirectFlag() {
		return directFlag;
	}

	public void setDirectFlag(String directFlag) {
		this.directFlag = directFlag;
	}

	/**
	 * @return the cllbList
	 */
	public List<TDictionary> getCllbList() {
		return cllbList;
	}

	/**
	 * @param cllbList the cllbList to set
	 */
	public void setCllbList(List<TDictionary> cllbList) {
		this.cllbList = cllbList;
	}

	/**
	 * @return the clmcList
	 */
	public List<TDictionary> getClmcList() {
		return clmcList;
	}

	/**
	 * @param clmcList the clmcList to set
	 */
	public void setClmcList(List<TDictionary> clmcList) {
		this.clmcList = clmcList;
	}

	/**
	 * @return the cldwList
	 */
	public List<TDictionary> getCldwList() {
		return cldwList;
	}

	/**
	 * @param cldwList the cldwList to set
	 */
	public void setCldwList(List<TDictionary> cldwList) {
		this.cldwList = cldwList;
	}

	/**
	 * @return the clxhList
	 */
	public List<TDictionary> getClxhList() {
		return clxhList;
	}

	/**
	 * @param clxhList the clxhList to set
	 */
	public void setClxhList(List<TDictionary> clxhList) {
		this.clxhList = clxhList;
	}

	/**
	 * @return the ckmcList
	 */
	public List<TDictionary> getCkmcList() {
		return ckmcList;
	}

	/**
	 * @param ckmcList the ckmcList to set
	 */
	public void setCkmcList(List<TDictionary> ckmcList) {
		this.ckmcList = ckmcList;
	}

	private void initClList() throws AppException{
		DictionaryService dicService = (DictionaryService) SpringUtil.getInstance().getBean("dicService");
		cllbList = dicService.findList("CLLB");
		clmcList = dicService.findList("CLMC");
		cldwList = dicService.findList("CLDW");
		clxhList = dicService.findList("CLXH");
		ckmcList = dicService.findList("KFMC");
	}

	public String getCllb() {
		return cllb;
	}

	public void setCllb(String cllb) {
		this.cllb = cllb;
	}

	public String getGzrwdid() {
		return gzrwdid;
	}

	public void setGzrwdid(String gzrwdid) {
		this.gzrwdid = gzrwdid;
	}

	public List<TGzrwdRw> getListRwd() {
		return listRwd;
	}

	public void setListRwd(List<TGzrwdRw> listRwd) {
		this.listRwd = listRwd;
	}

	public String getClmcPram() {
		return clmcPram;
	}

	public void setClmcPram(String clmcPram) {
		this.clmcPram = clmcPram;
	}

	
}
